<template>
  <base-breadcrumb :title="title">
    <!-- 质量创优奖项详情 -->
    <a-form-model
      ref="ruleForm"
      :model="form"
      :label-col="{ span: 6 }"
      :wrapper-col="{ span: 18 }"
      :rules="editModel != 0 ? rules : {}"
      id="base-breadcrumb"
      style="position: relative"
    >
      <a-spin :spinning="loading">
        <!-- 基础信息 -->
        <a-card :bordered="false" style="margin-bottom: 50px">
          <title-name title="基础信息"></title-name>
          <custom-form :formList="formList" :form="form" :editModel="editModel == 0 ? 'V' : ''"></custom-form>
          <title-name title="申报要求相关附件"></title-name>
          <UploadTableNew
            ref="UploadTableNew"
            :disabled="editModel == 0"
            :init-file-list="form.reviewFileArrays"
            business-code="QM11"
          />
        </a-card>
      </a-spin>
      <!-- 页脚操作栏 -->
      <div class="tool-btns--fixed btn_box page-btn-right-top">
        <a-button :loading="loading" v-if="editModel != 0" type="primary" class="button_right" @click="handleSubmit"
          >提交</a-button
        >
        <a-button type="primary" @click="$multiTab.close()">关闭</a-button>
      </div>
    </a-form-model>
  </base-breadcrumb>
</template>

<script>
import { excellenceTable } from '@/api/quality/quality' // api
import BaseEditPage from '@/views/baseEditPage' // 编辑详情页基础配置
import Selector from '@/components/Select/Selector' // 分包商

export default {
  name: 'a' + Date.now(),
  extends: new BaseEditPage(),
  components: {},
  data() {
    return {
      loading: false, // 加载loading
      editModel: 0, // 0 查看；1 编辑；2 新增
      form: {
        filler: void 0,
        fillerDate: void 0,
        reviewFileArrays: []
      },
      rules: {
        prizeNameId: [{ required: true, message: '请选择奖项名称', trigger: 'change' }],
        prizeTypeId: [{ required: true, message: '请选择奖项类别', trigger: 'change' }],
        prizeLevelId: [{ required: true, message: '请选择奖项等级', trigger: 'change' }],
        reviewMonth: [{ required: true, message: '请选择评审时间', trigger: 'change' }],
        phone: [{ required: true, whitespace: true, message: '请输入联系方式', trigger: 'change' }],
        directorUnit: [{ required: true, whitespace: true, message: '请输入主管单位', trigger: 'change' }],
        reviewStatusId: [{ required: true, message: '请选择评审状态', trigger: 'change' }],
        fileArrays: [{ required: true, type: 'array', message: '请上传图片', trigger: 'change' }]
      }, // 表单校验规则
      titles: ['查看质量创优奖项', '编辑质量创优奖项', '新增质量创优奖项'] // 标题
    }
  },
  mounted() {
    // 不为添加时请求详情
    if (this.editModel != 2) {
      this.loading = true
      excellenceTable
        .getDetail({ id: this.id })
        .then((res) => {
          this.loading = false
          if (res.code === 200) {
            res.data.reviewFileArrays = res.data.reviewFileArrays || []
            res.data.prizeFileArrays = res.data.prizeFileArrays || []
            this.form = res.data
          }
        })
        .catch(() => {
          this.loading = false
        })
    } else {
      this.form.filler = this.$userInfo()?.name // 默认填报人
      this.form.fillerDate = this.$dateFormat(new Date(), 'D') // 默认填报时间
    }
  },
  computed: {
    // 表单配置项
    formList() {
      return [
        {
          keyName: 'prizeNameId',
          viewName: 'name',
          type: Selector,
          label: '奖项名称',
          attrs: {
            selectType: '52'
          }
        },
        {
          type: Selector,
          keyName: 'prizeTypeId',
          viewName: 'prizeTypeName',
          label: '奖项类别',
          attrs: {
            selectType: '49'
          }
        },
        {
          type: Selector,
          keyName: 'prizeLevelId',
          viewName: 'prizeLevelName',
          label: '奖项等级',
          attrs: {
            selectType: '50'
          }
        },
        {
          keyName: 'reviewMonth',
          type: 'a-select',
          placeholder: '请选择评审月份',
          label: '评审时间',
          data: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12].map((i) => ({
            label: i + '月',
            value: i + '月'
          }))
        },
        {
          keyName: 'phone',
          type: 'a-input',
          label: '主管单位联系方式'
        },
        {
          keyName: 'directorUnit',
          type: 'a-input',
          label: '主管单位'
        },
        {
          type: Selector,
          keyName: 'reviewStatusId',
          viewName: 'reviewStatusName',
          label: '评审状态',
          attrs: {
            selectType: '51'
          }
        },
        {
          keyName: 'website',
          type: 'a-input',
          label: '主管单位网址'
        },
        {
          keyName: 'filler',
          type: 'a-input',
          disabled: true,
          label: '创建人'
        },
        {
          keyName: 'fillerDate',
          type: 'a-input',
          disabled: true,
          label: '创建时间'
        },
        {
          keyName: 'prizeExplain',
          type: 'BaseTextarea',
          label: '创奖说明',
          span: 24
        },
        {
          keyName: 'prizeFileArrays',
          type: 'uploadFile',
          businessCode: 'QM11',
          hiddenPrompt: true,
          attrs: {
            disabled: this.editModel == '0',
            editModel: this.editModel
            // uploadType: 'image'
          },
          label: '奖项说明',
          span: 24
        }
      ]
    }
  },
  methods: {
    /**
     * 提交表单
     */
    handleSubmit() {
      let that = this
      let name = ['updatePost', 'create'][that.editModel - 1] // 根据操作切换url地址
      // 校验表单
      that.$refs.ruleForm.validate((e) => {
        if (e) {
          if (!that.form.reviewFileArrays?.length) {
            this.$message.warning('申报要求相关附件不能为空！')
            return
          }
          that.$confirm({
            title: '温馨提示',
            content: '确定提交，提交后不可撤回？',
            onOk: () => {
              let form = that.$clone(that.form)
              // 新增时
              if (that.editModel == 2) {
                // 根据登陆人级别传不同的组织id
                form[{ CO: 'companyId', BC: 'branchComId' }[that.$currentPost().code]] = that.$currentPost().id
                // 添加登陆人级别
                form.submitLevel = that.$currentPost().code
              }
              return excellenceTable[name](form).then((res) => {
                if (res.code === 200 || res.data.code === 200) {
                  that.$message.success('提交成功！')
                  that.$multiTab.close()
                }
              })
            }
          })
        }
      })
    }
  }
}
</script>
<style lang="less" scoped>
/deep/ .ant-calendar-picker {
  width: 100% !important;
}
</style>